package com.ihealthway.all.groupbuy.modules.admin;

import com.ihealthway.all.slog.annotation.SLog;
import com.ihealthway.all.groupbuy.services.ImageLibraryService;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.groupbuy.vo.ImageLibrary;
import com.ihealthway.all.commons.Constants;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Shop ad module class
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/admin/imglib")
public class ImageLibraryModule {

    private static final Log logger = Logs.get();

    @Inject
    private ImageLibraryService imgLibService;
    @Inject
    private PropertiesProxy props;

    @At
    @Ok("beetl:admin/shopad.jsp")
    public void shopAd(HttpServletRequest req) {
        req.setAttribute("adInfo", imgLibService.getShopAdImgs());
    }

    @At
    @Ok("beetl:admin/images.jsp")
    public void goodImgs(HttpServletRequest req) {
    }

    @At
    public Map<String, Object> getUnUsedImgs(@Param("cId") Integer companyId) {
        return imgLibService.getUnUsedData(companyId);
    }

    @At
    @POST
    @SLog(module=Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_UPDATE, content="更新商城首页广告信息")
    public AjaxReturn updateShopAd(@Param("..")NutMap nm) {

        AjaxReturn ar = Ajax.ok();
        ar.setData("ok");
        imgLibService.updateShopAdImg(nm);
        return ar;
    }

    @At
    @AdaptBy(type = UploadAdaptor.class, args = { "ioc:uploadImg" })
    @SLog(module=Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_ADD, content="新增商品图片【${args[0]['id']}-${args[0]['name']}】信息")
    public AjaxReturn ajaxUploadImg(@Param("..")NutMap nm) {
        AjaxReturn ar = Ajax.ok();
        String savePath = props.get(Constants.UPLOAD_ATTACHMENT_SAVE_PATH) + props.get(Constants.GOODS_IMG_FOLDER);
        imgLibService.uploadGoodsImg(nm, savePath);
        return ar;
    }

    @At
    @AdaptBy(type = UploadAdaptor.class, args = { "ioc:uploadImg" })
    @SLog(module=Constants.MODULE_SHOP, type=Constants.OPERATE_TYPE_UPDATE, content="更新商城首页广告图片【${args[0]['orderNo']}】信息")
    public AjaxReturn ajaxUploadAd(@Param("..")NutMap nm, HttpServletRequest req, ServletContext context) {
        AjaxReturn ar = Ajax.ok();
        String savePath = props.get(Constants.UPLOAD_ATTACHMENT_SAVE_PATH) + props.get(Constants.SHOP_AD_FOLDER);
        ImageLibrary il = imgLibService.uploadShopAdImg(nm, savePath);
        ar.setData(il);
        return ar;
    }

    @At
    @POST
    public Map<String, Object> dataList(int page, int rows) {
        return imgLibService.getData(rows, page);
    }

}
